<template>
  <div class="swiper">
    <swiper :options="swiperOption" ref="mySwiper">
        <swiper-slide v-for="(item, itemIndex) in data" :key="itemIndex" v-if="item.image_url">
          <img :style="imgStyle" :src="item.image_url" />
        </swiper-slide>
      <div class="swiper-pagination" slot="pagination"></div>
    </swiper>
    <slot></slot>
  </div>
</template>

<script>
import { swiper, swiperSlide } from "vue-awesome-swiper";
import "swiper/dist/css/swiper.css";

export default {
  props: {
    swiperOption: {
      type: Object,
      default: {
        loop: true,
        autoplay: {
          delay: 500,
          disableOnInteraction: false
        },
        pagination: {
          el: ".swiper-pagination"
        }
      }
    },
    data: {
      type: Array,
      default: []
    },
    imgStyle: {
      type: Object,
      default: {
        width: "100%",
        height: "180px"
      }
    },
    refreshDelay: {
      type: Number,
      default: 20
    }
  },
  components: {
    swiper,
    swiperSlide
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.swiper;
    }
  },
  mounted() {
    setTimeout(() => {
      this.update();
    }, this.refreshDelay);
  },
  methods: {
    update() {
      console.log("swiper refresh");
      this.swiper && this.swiper.update();
      //   this.swiper.slideTo(1, 1000, false);
    }
  },
  watch: {
    data() {
      setTimeout(() => {
        this.update();
      }, this.refreshDelay);
    }
  }
};
</script>

<style scoped lang="less">
</style>
